package com.ybear.ybcomponent.widget.shape;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TableLayout;

import androidx.annotation.ColorInt;

import com.ybear.ybcomponent.R;

public class ShapeTableLayout extends TableLayout implements IShape {
    private ShapeHelper mHelper;

    public ShapeTableLayout(Context context) {
        this(context, null);
    }

    public ShapeTableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHelper = new ShapeHelper();
        initTypeArray( context, attrs );
        mHelper.init( this );
    }

    private void initTypeArray(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(
                attrs, R.styleable.ShapeTableLayout
        );
        //形状类型（圆角、圆形）
        setShape(typedArray.getInt(
                R.styleable.ShapeTableLayout_shape, Shape.ROUND_RECT
        ));
        //圆角半径
        setRadius(typedArray.getDimensionPixelSize(
                R.styleable.ShapeTableLayout_shapeRadius, 0
        ));
        //描边大小
        setBorderSize(typedArray.getDimensionPixelSize(
                R.styleable.ShapeTableLayout_shapeBorderSize, 0
        ));
        //描边颜色
        setBorderColor(typedArray.getColor(
                R.styleable.ShapeTableLayout_shapeBorderColor, Color.WHITE
        ));
        //阴影半径
        setShadowRadius(typedArray.getDimensionPixelSize(
                R.styleable.ShapeTableLayout_shapeShadowRadius, 0
        ));
        //阴影颜色
        setShadowColor(typedArray.getColor(
                R.styleable.ShapeTableLayout_shapeShadowColor, 0
        ));
        //阴影X偏移
        setShadowOffsetX(typedArray.getDimensionPixelSize(
                R.styleable.ShapeTableLayout_shapeShadowOffsetX, 0
        ));
        //阴影Y偏移
        setShadowOffsetY(typedArray.getDimensionPixelSize(
                R.styleable.ShapeTableLayout_shapeShadowOffsetY, 0
        ));
        //复用
        typedArray.recycle();
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        mHelper.dispatchDraw( canvas, this );
    }

    @Override
    public void setShape(@Shape int shape) { mHelper.setShape( shape ); }
    @Override
    public void setRadius(int radius) { mHelper.setRadius( radius ); }
    @Override
    public void setBorderSize(int size) { mHelper.setBorderSize( size ); }
    @Override
    public void setBorderColor(int color) { mHelper.setBorderColor( color ); }
    @Override
    public void setShadowRadius(int radius) { mHelper.setShadowRadius( radius ); }
    @Override
    public void setShadowColor(@ColorInt int color) { mHelper.setShadowColor( color ); }
    @Override
    public void setShadowOffsetX(int offsetX) { mHelper.setShadowOffsetX( offsetX ); }
    @Override
    public void setShadowOffsetY(int offsetY) { mHelper.setShadowOffsetY( offsetY ); }

    @Override
    public void setEnableLayerTypeHardware(boolean enable) {
        mHelper.setEnableLayerTypeHardware( enable );
    }
}
